@extends('layouts.admin')

@section('styles')
parent
{{ HTML::style('css/jquery-ui.min.css') }}
@stop

@section('content')
<div class="page-header">
	<h2>Department - 
		<small>
		@if($method == 'post')
			Add a new department
		@elseif($method == 'delete')
			Delete {{$department->department_name}} user ?
		@else
			Edit {{$department->department_name}} user
		@endif
		</small>
	</h2>
</div>
@if ($message = $errors->first('message'))
<div class="row">
	<div class="col-lg-12">
		<div class="alert alert-danger alert-block">
			<button type="button" class="close" data-dismiss="alert">&times;</button>
			<h4>Unsuccess</h4>
			{{{ $message }}}
		</div>
	</div>
</div>
@endif

<div class="row">
	<div class="col-lg-12">
		{{Form::model($department, array('method' => $method, 'url'=>'departments/'.$department->id, 'class' => 'form-horizontal'))}}
		@unless($method == 'delete')
			<!-- Short Department Name -->
			<div class="form-group {{{ $errors->has('short_department_name') ? ' has-error' : '' }}}">
				<div class="col-sm-4">
					{{ Form::label('short_department_name', 'Short Department Name', array('class' => 'control-label')) }}
					{{ Form::text('short_department_name', Input::old('short_department_name'), array('class' => 'form-control')) }}
				</div>
			</div>

			<!-- Department Name -->
			<div class="form-group {{{ $errors->has('department_name') ? ' has-error' : '' }}}">
				<div class="col-sm-4">
					{{ Form::label('department_name', 'Department Name', array('class' => 'control-label')) }}
					{{ Form::text('department_name', Input::old('department_name'), array('class' => 'form-control')) }}
				</div>
			</div>
			
			<!-- Department Name -->
			<div class="form-group {{{ $errors->has('department_description') ? ' has-error' : '' }}}">
				<div class="col-sm-4">
					{{ Form::label('department_description', 'Department Description ', array('class' => 'control-label')) }}
					{{ Form::textarea('department_description', Input::old('department_description'), array('size' => '30x5', 'class' => 'form-control')) }}
				</div>
			</div>
			
			<!-- PDF Password -->
			<div class="form-group {{{ $errors->has('pdf_password') ? ' has-error' : '' }}}">
				<div class="col-sm-4">
					{{ Form::label('pdf_password', 'PDF Password', array('class' => 'control-label')) }}
					{{ Form::text('pdf_password', Input::old('pdf_password'), array('class' => 'form-control')) }}
				</div>
			</div>
			
			<!-- Template Password -->
			<div class="form-group {{{ $errors->has('templates') ? ' has-error' : '' }}}">
				<div class="col-sm-4">
					{{ Form::label('templates', 'Template Name', array('class' => 'control-label')) }}
					{{ Form::textarea('templates', (implode(', ', $department->templates->lists('template_name'))) , array('size' => '30x5', 'class' => 'form-control')) }}
				</div>
			</div>
			
			<div class="form-group">
				<div class="col-sm-4">
					{{Form::submit("Save", array("class"=>"btn btn-success"))}}
					<a href="{{url('departments')}}" class="btn btn-default">
						Cancel
					</a>
				</div>
			</div>
			
		@else
			<div class="form-group">
				<div class="col-sm-4">
					{{Form::submit("Delete", array("class"=>"btn btn-success"))}}
					<a href="{{url('departments')}}" class="btn btn-default">
						Cancel
					</a>
				</div>
			</div>
		@endif
		{{Form::close()}}
	</div>
</div>
@stop

@section('endscript')
@parent
{{ HTML::script('js/jquery-ui.min.js') }}

<script>
  $(function() {
    var availableTags = [{{ $template_options }}];
    function split( val ) {
      return val.split( /,\s*/ );
    }
    function extractLast( term ) {
      return split( term ).pop();
    }
 
    $( "#templates" )
      // don't navigate away from the field on tab when selecting an item
      .bind( "keydown", function( event ) {
        if ( event.keyCode === $.ui.keyCode.TAB &&
            $( this ).autocomplete( "instance" ).menu.active ) {
          event.preventDefault();
        }
      })
      .autocomplete({
        minLength: 0,
        source: function( request, response ) {
          // delegate back to autocomplete, but extract the last term
          response( $.ui.autocomplete.filter(
            availableTags, extractLast( request.term ) ) );
        },
        focus: function() {
          // prevent value inserted on focus
          return true;
        },
        select: function( event, ui ) {
          var terms = split( this.value );
          // remove the current input
          terms.pop();
          // add the selected item
          terms.push( ui.item.value );
          // add placeholder to get the comma-and-space at the end
          terms.push( "" );
          this.value = terms.join( ", " );
          return false;
        }
      });
  });
  </script>
@stop

